package com.xjh;

import cn.hutool.core.util.IdUtil;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.xjh.t.common.Constast;
import com.xjh.t.domain.OrderInfo;
import com.xjh.t.domain.User;
import com.xjh.t.mapper.OrderInfoMapper;
import com.xjh.t.service.IGroupAttrInfoService;
import com.xjh.t.service.IOrderInfoService;
import com.xjh.t.service.IUserService;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.junit.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@MapperScan(basePackages = {"com.xjh.t.mapper"})
//@ServletComponentScan(basePackages = {"com.xjh.t.filter"})
public class ApplicationWeb {

    @Autowired
    IUserService userService;
    public static void main(String[] args) {

        SpringApplication.run(ApplicationWeb.class, args);

    }

    @Test
    public void getAnnotation() throws NoSuchFieldException, IllegalAccessException {
        DS annoTable = IGroupAttrInfoService.class.getAnnotation(DS.class);

        if (annoTable == null) {
            throw new RuntimeException("please add @DS for Test");
        }
        else
        {
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(annoTable);
// 过去私有 memberValues 属性
            Field f = invocationHandler.getClass().getDeclaredField("memberValues");
            f.setAccessible(true);
// 获取实例的属性map
            Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
// 修改属性值
            memberValues.put("value", "Ord");
        }
    }

    @Test
    public void TestConnectData() throws Exception
    {


        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/scfy?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("11223344");

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:/mapper/orderxml/*Mapper.xml");

        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setDataSource(dataSource);

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();

        SqlSession sqlsession = sqlSessionFactory.openSession();
        OrderInfoMapper orderInfoMapper = sqlsession.getMapper(OrderInfoMapper.class);
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();

       // queryWrapper.eq("or_id","18014398509481986");


// 调用数据库操作方法
        sqlsession.close();
    }

    @Test
    public void testMonth()
    {

        User user=new User();
        user.setId(1);
        String salt= IdUtil.simpleUUID().toUpperCase();
        user.setSalt(salt);//设置盐
        user.setPwd(new Md5Hash(Constast.USER_DEFAULT_PWD, salt, 2).toString());//设置密码

       // IUserService userService =
       // this.userService.updateById(user);

        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //Date date = sdf.parse("2019-02-02");
        Calendar c = Calendar.getInstance();
        //设置为指定日期
        c.setTime(new Date());
        //指定日期月份减去一
        c.add(Calendar.MONTH, -2);
        //指定日期月份减去一后的 最大天数
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
        //获取最终的时间
        Date lastDateOfPrevMonth = c.getTime();
        System.out.println("上月最后一天：" + sdf.format(lastDateOfPrevMonth));


        c.setTime(new Date());
        c.set(Calendar.DAY_OF_MONTH,1);
        c.add(Calendar.MONTH,-2);

        lastDateOfPrevMonth = c.getTime();
        System.out.println("上月第一天：" + sdf.format(lastDateOfPrevMonth));*/

    }
}
